package gee

import "net/http"

type HandlerFunc func(ctx *Context)

type Engine struct {


	router *router
}

func New() *Engine {
	// 返回一个 路由实例
	return &Engine{router: newRouter()}
}
// 注册一个路由， 这里实际调用 路由实例的注册路由方法
func (engine *Engine) addRoute(method string, pattern string, handler HandlerFunc) {
	engine.router.addRoute(method, pattern, handler)
}
// 注册 GET
func (engine *Engine) GET(pattern string, handler HandlerFunc) {
	engine.addRoute("GET", pattern, handler)
}

// 注册 POST

func (engine *Engine) POST(pattern string, handler HandlerFunc) {
	engine.addRoute("POST", pattern, handler)
}

func (engine *Engine) Run(addr string) (err error) {
	return  http.ListenAndServe(addr, engine)
}

func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	// 封装会话
	c := newContext(w, req)
	// 将会话送到路由，解析出对应的处理逻辑
	engine.router.handle(c)
}



